package com.example.springback.controller;

import com.example.springback.common.JWTUtil;
import com.example.springback.common.Result;
import com.example.springback.domain.Employee;

import com.example.springback.domain.Enterprise;
import com.example.springback.service.EmployeeService;
import com.example.springback.service.EnterPriseService;
import com.example.springback.service.PensionService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {
    @Resource
    private EnterPriseService enterPriseServicer;
    @Resource
    private PensionService pensionService;
    @Resource
    private EmployeeService employeeService;

    //企业登录
     @PostMapping("/login")
    public Result login(@RequestBody Enterprise enterprise){
        Enterprise enterprise1=enterPriseServicer.login(enterprise);
         // 登录成功，生成 token
         String token = JWTUtil.createToken(enterprise1.getUsername());
         // 自定义一个返回对象
         Map<String, Object> result = new HashMap<>();
         result.put("token", token);
         result.put("user", enterprise1);
         return Result.success(result);
    }
    //企业注册
    @PostMapping("/register")
    public Result register(@RequestBody Enterprise enterprise){
        Enterprise enterprise1=enterPriseServicer.register(enterprise);
        return Result.success(enterprise1);
    }

    @GetMapping("/searchPage")
    public Result selectPage(Enterprise enterprise , @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<Enterprise> enterprisePageInfo =enterPriseServicer.selectPage(enterprise,pageNumber,pageSize);
        return Result.success(enterprisePageInfo);
    }
    //新增员工
    @PostMapping("/add")
    public Result add(@RequestBody Enterprise enterprise){
        enterPriseServicer.add(enterprise);
        return Result.success();
    }
    //更新员工信息
    @PostMapping ("/update")
    public Result update(@RequestBody Enterprise enterprise){
       Enterprise enterprise1= enterPriseServicer.update(enterprise);
        return Result.success(enterprise1);
    }
    //删除企业
    @PostMapping ("/delete")
    public Result delete(@RequestBody Enterprise enterprise){
        enterPriseServicer.delete(enterprise.getEnterpriseId());
        return Result.success();
    }
    @PostMapping("/deletebatch")
    public Result deleteBatch(@RequestBody List<Enterprise> enterprises){
        enterPriseServicer.deleteBatch(enterprises);
        return Result.success();
    }
    //后台管理页面的kpi数据
    @GetMapping("/kpi")
    public Result getKpiData(String month) {
            int enterpriseNumber = enterPriseServicer.countAll();//
            int peopleNumber = employeeService.countPeople(); //统计参保人数
            double payNumber = pensionService.sumAllPension(month);
            Map<String, Object> data = new HashMap<>();
            data.put("enterpriseNumber", enterpriseNumber);
            data.put("peopleNumber", peopleNumber);
            data.put("payNumber", payNumber);
            return Result.success(data);
    }
}
